
export default {
    namespace: 'report',
    state: {
        reportData: {}
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *init({ payload }, { put, call, select }){
            const {month, callback, carNumber} = payload;
            const reportData = yield put.resolve({
                type: '$request/post',
                payload: {
                    url : '/api/monthReport',
                    data: {month,carNumber}
                }
            });
            reportData.totalPrice = reportData.totalPrice/100;
            reportData.averagePrice = reportData.averagePrice/100;
            Object.keys(reportData).forEach(key => {
                if(reportData[key]){
                    if(['averagePrice','averageAmount'].includes(key)){
                        reportData[key] = reportData[key].toFixed(2);
                    }
                }else{
                    reportData[key] = '--';
                }
            });
            callback(reportData);
            yield put({
                type : 'save',
                payload : {reportData}
            });
            
        },
        
    }
};